package com.kgc.app.controller;

import com.kgc.app.dto.AppVersionDto;
import com.kgc.app.pojo.AppVersion;
import com.kgc.app.pojo.DevUser;
import com.kgc.app.service.AppVersionService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/appversion")
public class AppVersionController {

    //引入appversion的service层对象
    @Autowired
    private AppVersionService appVersionService;



    @RequestMapping("/toSelectVersionByAppId/{appId}")
    public String toAppVersion(@PathVariable Long appId, Model model){

        List<AppVersionDto> appVersionDtos = appVersionService.selectAppVersionByAppId(appId);
        model.addAttribute("appVersionDtos",appVersionDtos);
        model.addAttribute("appId",appId);
        //查询到对应的APP版本信息后，跳转到APP版本的添加页面
        return "developer/appversionadd";
    }



    @RequestMapping("/add")
    public String addAppVersion(AppVersion appVersion,HttpSession session){

        DevUser devUser = (DevUser)session.getAttribute("devUser");

        appVersion.setModifyBy(devUser.getId());
        appVersion.setModifyDate(new Date());

        appVersionService.addAppVersion(appVersion);
        System.out.println(appVersion.getAppId());

        //添加完版本后，还是回到添加页面，但是要把新添加的版本回显出来
        return "redirect:/appversion/toSelectVersionByAppId/"+appVersion.getAppId();
    }



    /**
     * 异步文件上传
     * @param a_downloadLink 文件上传项的名称
     * @param session
     * @return
     */
    @RequestMapping("ajaxFileUpload")
    @ResponseBody
    public Map<String,Object> ajaxFileUpload(MultipartFile a_downloadLink, HttpSession session){
        //创建一个map集合用于存储返回给页面的信息
        Map<String, Object> map = new HashMap<String, Object>();

        //验证文件类型
        //获取上传的文件名
        String originalFilename = a_downloadLink.getOriginalFilename();
        //然后得到这个文件的后缀
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //判断文件后缀是否符合要求，如果不符合则返回错误信息
        if (!suffixName.equalsIgnoreCase(".apk")){
            map.put("code",500);
            map.put("msg","文件格式错误");
            return map;
        }


        //开始进行文件上传操作
        /**
         * 创建一个新的文件名称
         *      randomUUID      生成一个通用唯一识别码（中间包含"-"）
         *      replace         去掉通用唯一识别码中间的"-"
         */

        System.out.println(originalFilename);

        //指定上传路径
        ServletContext application = session.getServletContext();
        String realPath = application.getRealPath("/fileUpload/apk");

        //生成上传文件夹
        File realPathFile = new File(realPath);
        //判断此文件夹是否存在
        if (!realPathFile.exists()){
            //不存在则创建一个
            realPathFile.mkdirs();
        }

        //生成上传文件对象
        File file = new File(realPathFile, originalFilename);
        try {
            a_downloadLink.transferTo(file);//上传完成
            map.put("code",200);
            map.put("msg","文件上传成功！");
            map.put("filePath","/fileUpload/apk/"+originalFilename);
            map.put("apkFileName",originalFilename);
        } catch (IOException e) {
            map.put("code",500);
            map.put("msg","文件上传失败！");
            e.printStackTrace();
        }

        return map;
    }



}
